package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.SecurityList;
import com.example.prison.entity.vo.PatrolTrajectoryVO;
import com.example.prison.entity.vo.SecurityListVO;
import com.example.prison.service.SecurityListService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@Api(tags = "安防记录")
@RestController
@RequestMapping("/api")
public class SecurityListController {

    @Autowired
    private SecurityListService securityListService;

//    @Autowired
//    private SystemLogService systemLogService;

    @ApiOperation("获取安防记录")
    @GetMapping("getSecurityList")
    public Response<PageInfo<SecurityList>> getSecurityList(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<SecurityList> pageInfo = securityListService.getSecurityList(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取安防记录及信息")
    @GetMapping("getSecurityListAndInfo")
    public Response<PageInfo<SecurityListVO>> getSecurityListAndInfo(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                     @RequestParam(value = "sn") String sn,
                                                                     @RequestParam(value = "beginTime", defaultValue = "0000-00-00 00:00:00") String beginTime,
                                                                     @RequestParam(value = "endTime", defaultValue = "9999-12-31 23:59:59") String endTime) {
        PageInfo<SecurityListVO> pageInfo = securityListService.getInfoList(pageNumber, pageSize, sn, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

//    @ApiOperation("依据时间获取安防记录")
//    @GetMapping("getSecurityListByTime")
//    public Response<PageInfo<SecurityListVO>> getSecurityListByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                            @RequestParam(value = "beginTime", defaultValue = "0000-00-00 00:00") String beginTime,
//                                                            @RequestParam(value = "endTime", defaultValue = "9999-12-31 23:59") String endTime) {
//        beginTime = beginTime + ":00";
//        endTime = endTime + ":00";
//        if(beginTime.compareTo(endTime) > 0 ) {
//            return Response.error("时间输入错误！");
//        }
//        PageInfo<SecurityListVO> pageInfo = securityListService.getListByTime(pageNumber, pageSize, beginTime, endTime);
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("删除安防记录")
    @PostMapping("/deleteSecurityList")
    public Response<?> deleteAttendanceList(@Parameter(description = "安防记录ID") @RequestParam Integer[] ids) {
        Integer[] res = {};
        for(Integer id : ids) {
            if (ObjectUtils.isEmpty(securityListService.getById(id))) {
                res = Arrays.copyOf(res,res.length+1);
                res[res.length-1] = id;
            } else {
                securityListService.delete(ids);
            }
        }
        if(ObjectUtils.isNotEmpty(res)) {
            return Response.info("安防记录id为"+ Arrays.toString(res) +"的记录不存在！",Arrays.toString(res));
        }
        log.info("删除安防记录");
//        systemLogService.addLog("删除安防记录");
        return Response.success("删除成功！");
    }

//    @ApiOperation("依据设备编号获取安防记录")
//    @GetMapping("getSecurityListBySN")
//    public Response<PageInfo<SecurityListVO>> getSecurityListBySN(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                    @RequestParam(value = "sn") String sn) {
//        PageInfo<SecurityListVO> pageInfo = securityListService.getListBySN(pageNumber, pageSize, sn);
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("当天安防巡更记录")
    @GetMapping("getPatrolTrajectory")
    public Response<PageInfo<PatrolTrajectoryVO>> getPatrolTrajectory(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<PatrolTrajectoryVO> pageInfo = securityListService.getPatrolTrajectory(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

}
